﻿Imports FinanceiroBLL
Imports FinanceiroDAL
Imports VendasBLL
Public Class Contas_ReceberFRM
    Public Op As Char
    Public var As Integer
    Public codigovenda As Integer 'variavel para ser passado de vendas
    Dim parcela, codccc, st As Integer
    Dim tab As DataTable
    Dim aux As Integer
    Dim objBLL As ContasReceberBLL
    Public ObjVendas As New VendaBLL
    Dim refnota, status, codigo As Integer
    Public finaliza As String
    Dim valorparcela As Decimal 'valortotal
#Region "Validating"
    Private Sub CBFRecebimento_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles CBFRecebimento.Validating
        If (CBFRecebimento.SelectedValue > 0) Then
            If (CBFRecebimento.SelectedValue = 1 Or CBFRecebimento.SelectedValue = 3 Or CBFRecebimento.SelectedValue = 4 Or CBFRecebimento.SelectedValue = 8) Then 'prazo
                BParcelamento.Visible = True
                '   RBVista.Checked = True
            Else
                BParcelamento.Visible = False
                '  RBPrazo.Checked = False
            End If
            If CBFRecebimento.SelectedValue = 2 Or CBFRecebimento.SelectedValue = 5 Or CBFRecebimento.SelectedValue = 6 Or CBFRecebimento.SelectedValue = 7 Or CBFRecebimento.SelectedValue = 9 Then 'vista
                BParcelamento.Visible = False
                '  RBVista.Checked = True
            End If

            If CBFRecebimento.SelectedValue = 1 Or CBFRecebimento.SelectedValue = 2 Or CBFRecebimento.SelectedValue = 5 Or CBFRecebimento.SelectedValue = 7 Or CBFRecebimento.SelectedValue = 9 Then
                TXTBolCheque.ReadOnly = True
            ElseIf CBFRecebimento.SelectedValue = 3 Or CBFRecebimento.SelectedValue = 4 Then
                TXTBolCheque.ReadOnly = False
            End If
        End If
    End Sub
#End Region
#Region "Load"
    Private Sub Contas_ReceberFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        limpar()
        If Op = "N" Then
            CarregaCombo()
        End If
        If Op = "M" Then
            CarregaCombo()
        End If
        If (Op = "R") Then
            'carregar vend
            CarregaCombo()
            Dim objcli = New ClientesBLL(ObjVendas.CodCliente)
            TXTCliente.Text = objcli.Nome
            TXTNumref.Text = ObjVendas.VenCodigo
            TXTValorTitulo.Text = ObjVendas.Total
            TXTValorTitulo_Leave(Nothing, Nothing)
            DTPEmissao.Value = ObjVendas.Data
            BLocaliza.Enabled = False
            aux = objcli.Codigo
        End If

    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        LCodigo.Text = ""
        TXTNumref.Text = ""
        CBFRecebimento.SelectedValue = -1
        DTPEmissao.Text = ""
        TXTCliente.Text = ""
        TXTValorTitulo.Text = "0,00"
        TXTDesconto.Text = "0,00"
        TXTJuros.Text = "0,00"
        TXTValorParcela.Text = "0,00"
        CBContas.SelectedValue = -1
        TXTBolCheque.Text = ""
        objBLL = Nothing
    End Sub
#End Region
#Region "Carrega combo"
    Sub CarregaCombo()
        objconexao.carregaCombo(CBFRecebimento, "Generico.Gen_FormaPagamento", "pag_codigo", "pag_descricao", "sta_codigo =1", "")
        objconexao.carregaCombo(CBContas, "Financeiro.Ofi_Contas", "con_codigo", "con_descricao", "sta_codigo=1", "")
        CBFRecebimento.SelectedValue = -1
        CBContas.SelectedValue = -1
    End Sub
#End Region
#Region "Localiza"
    Private Sub BLocaliza_Click(sender As System.Object, e As System.EventArgs) Handles BLocaliza.Click
        Try
            Dim objBLL As CentroCustoBLL
            objBLL = New CentroCustoBLL
            Dim frmcli As New ClientesCON
            frmcli.tipo = "ret"
            frmcli.ShowDialog()
            TXTCliente.Text = frmcli.cliente
            aux = frmcli.codcli
            '  tab = objBLL.localiza(aux)
            ' TXTCentroCusto.Text = tab.Rows(0)("ccc_descricao").ToString()
            ' codccc = tab.Rows(0)("ccc_codigo").ToString()
            frmcli.Dispose()
            frmcli = Nothing
            objBLL = Nothing
        Catch ex As Exception
        End Try
    End Sub
#End Region
#Region "Valida"
    Sub valida()
        Try
            If (TXTNumref.Text <> "") Then
                codigovenda = TXTNumref.Text
            Else
                codigovenda = Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "Salvar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        valorparcela = TXTValorParcela.Text.Replace("R$", "")
        Try
            valida()
            If (RBPrazo.Checked = False And RBVista.Checked = True) Or (RBPrazo.Checked = True And RBVista.Checked = False) Then
                If TXTCliente.Text <> "" Then
                    If (CBFRecebimento.SelectedValue <> Nothing Or CBContas.SelectedValue <> Nothing) Then
                        If valorparcela > 0 Then
                            objBLL = New ContasReceberBLL(codigo, codigovenda, codccc, Date.Parse(DTPEmissao.Value), aux, valorparcela)
                            '(codigo, codccc, refnota, Date.Parse(DTPDtEmissao.Text), TXTObservacao.Text, TXTFornPrestador.Text)
                            Dim objitrecBLL As New itensCtasReceberBLL
                            If (st = 4) Then
                                objitrecBLL.icrdtrecebimento = Nothing
                            Else
                                objitrecBLL.icrdtrecebimento = DTPEmissao.Value 'ou txt
                            End If
                            objitrecBLL.icrvalor = TXTValorTitulo.Text
                            objitrecBLL.pagcodigo = CBFRecebimento.SelectedValue
                            objitrecBLL.icrdtvencimento = DTPVencimento.Text
                            objitrecBLL.icrvalrecebido = TXTValorParcela.Text 'campo total + jam -desc
                            objitrecBLL.icrdesconto = TXTDesconto.Text
                            objitrecBLL.icrjam = TXTJuros.Text
                            objitrecBLL.icrparcela = parcela 'num de parcela
                            objitrecBLL.icrobs = TXTObservacao.Text
                            objitrecBLL.concodigo = CBContas.SelectedValue
                            objitrecBLL.icrbolcheque = TXTBolCheque.Text
                            objitrecBLL.stacodigo = st
                            objBLL.lCtasreceber.Add(objitrecBLL)
                            var = objBLL.Inserir()
                            var = objBLL.ctrcodigo
                            MsgBox("Dados gravados com sucesso!", MsgBoxStyle.Information)
                            If (Not IsNothing(ObjVendas)) Then
                                If (ObjVendas.VenCodigo > 0) Then
                                    finaliza = "OK"
                                    Me.Close()
                                Else : Close()
                                End If
                            Else
                                Close()
                            End If

                            Close()
                        Else
                            MsgBox("Campo valor da parcela possui dados inválidos.", MsgBoxStyle.Information)
                            TXTValorParcela.Select()
                        End If
                    Else
                        MsgBox("Forma de recebimento ou conta não definidos, por favor verifique!", MsgBoxStyle.Information)
                    End If
                Else
                    MsgBox("Verificar campo Cliente ou Centro Custo.", MsgBoxStyle.Information)
                    TXTCliente.Select()
                End If
            Else
                MsgBox("Escolha uma condição de pagamento.", MsgBoxStyle.Information)
            End If
        Catch ex As Exception

        End Try
    End Sub
#End Region
#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
        limpar()
        objBLL = Nothing
    End Sub
#End Region
#Region "Parcelamento"
    Private Sub BParcelamento_Click(sender As System.Object, e As System.EventArgs) Handles BParcelamento.Click
        Decimal.TryParse(TXTValorParcela.Text.Replace("R$", ""), valorparcela)
        Try
            If (RBPrazo.Checked = False And RBVista.Checked = True) Or (RBPrazo.Checked = True And RBVista.Checked = False) Then
                If TXTCliente.Text <> "" Then
                    If (DTPVencimento.Value.ToShortDateString) >= Date.Now.ToShortDateString Then
                        If CBFRecebimento.SelectedValue <> 0 And CBContas.SelectedValue <> 0 Then
                            If TXTValorTitulo.Text <> "" And valorparcela <> 0 Then
                                TSBSalvar_Click(Nothing, Nothing)
                                Dim frmparc As New RecParcelamentosFRM
                                frmparc.codcli = var 'cod cliente
                                frmparc.Op = "C" 'contas receber
                                frmparc.nomecli = TXTCliente.Text
                                frmparc.codccc = codccc
                                frmparc.diavenc = DTPVencimento.Value 'data vencimento
                                frmparc.dtemissao = DTPEmissao.Value
                                frmparc.ValTotal = TXTValorParcela.Text
                                frmparc.formarecebida = CBFRecebimento.SelectedValue
                                frmparc.conta = CBContas.SelectedValue
                                frmparc.numparcela = parcela
                                frmparc.vendaref = Val(TXTNumref.Text)
                                frmparc.codctr = var
                                frmparc.bolcheque = TXTBolCheque.Text
                                frmparc.ShowDialog()
                                frmparc.Dispose()
                                frmparc = Nothing
                                limpar()
                            Else
                                MsgBox("Verifique o campo Valor do título.", MsgBoxStyle.Information)
                                TXTValorTitulo.Select()
                            End If
                        Else
                            MsgBox("Tipo de recebimento ou nome de conta não selecionado", MsgBoxStyle.Information)
                        End If
                    Else
                        MsgBox("Data de vencimento menor que data atual", MsgBoxStyle.Exclamation)
                        DTPVencimento.Select()
                    End If
                Else
                    MsgBox("Campo cliente não preenchido", MsgBoxStyle.Information)
                    TXTCliente.Select()
                End If
            Else
                MsgBox("Escolha uma condição de pagamento.", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
        End Try
    End Sub
#End Region
#Region "Propriedades"
    Private Sub TXTValorTitulo_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValorTitulo.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TXTDesconto.Select()
        End If
    End Sub

    Private Sub TXTDesconto_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTDesconto.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
        End If
    End Sub

    Private Sub TXTJuros_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTJuros.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TXTJuros.Text = FormatCurrency(TXTJuros.Text.Replace("R$", "").Replace(" ", ""))
        End If
    End Sub

    Private Sub TXTValorParcela_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValorParcela.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then

        End If
    End Sub

    Private Sub TXTValorTitulo_Leave(sender As System.Object, e As System.EventArgs) Handles TXTValorTitulo.Leave
        validacampos()
    End Sub

    Private Sub TXTDesconto_Leave(sender As System.Object, e As System.EventArgs) Handles TXTDesconto.Leave
        validacampos()
    End Sub

    Private Sub TXTJuros_Leave(sender As System.Object, e As System.EventArgs) Handles TXTJuros.Leave
        validacampos()
    End Sub

    Private Sub TXTValorParcela_Leave(sender As System.Object, e As System.EventArgs) Handles TXTValorParcela.Leave
        validacampos()
    End Sub
#End Region
#Region "ValidaCampos"
    Sub validacampos()
        Dim valorparcela As Decimal = 0
        Dim valtitulo As Decimal = 0
        Dim desc, jam As Decimal
        Try
            If (Decimal.TryParse(TXTValorTitulo.Text.Replace("R$", ""), valtitulo)) Then
            Else : valtitulo = "0,00"
            End If
            If (Decimal.TryParse(TXTDesconto.Text.Replace("R$", ""), desc)) Then
            Else : desc = ("0,00")
            End If
            If (Decimal.TryParse(TXTJuros.Text.Replace("R$", ""), jam)) Then
            Else : jam = "0,00"
            End If
            If (Decimal.TryParse(TXTValorParcela.Text.Replace("R$", ""), valorparcela)) Then
            Else : valorparcela = "0,00"
            End If

            If (desc > valtitulo) Then
                TXTValorParcela.Text = valtitulo - desc
                MsgBox("Valor de Desconto maior que o valor do título", MsgBoxStyle.Exclamation)
                TXTDesconto.SelectAll()
                TXTDesconto.Select()
            Else
                TXTValorParcela.Text = (valtitulo + jam) - desc
            End If
            If TXTValorTitulo.Text <> "" Then
                TXTValorTitulo.Text = FormatCurrency(TXTValorTitulo.Text)
            Else
                TXTValorTitulo.Text = "0,00"
            End If
            If TXTDesconto.Text <> "" Then
                TXTDesconto.Text = FormatCurrency(TXTDesconto.Text)
            Else
                TXTDesconto.Text = "0,00"
            End If
            If TXTJuros.Text <> "" Then
                TXTJuros.Text = FormatCurrency(TXTJuros.Text)
            Else
                TXTJuros.Text = "0,00"
            End If
            If DTPVencimento.Value < Date.Now.ToShortDateString Then
                MsgBox("Data de Vencimento menor que a data atual", MsgBoxStyle.Exclamation)
                DTPVencimento.Select()
            End If
            TXTValorParcela.Text = FormatCurrency(TXTValorParcela.Text)
        Catch ex As Exception
            MsgBox("Formato incorreto nos campos!")
        End Try
    End Sub
#End Region
#Region "Radio Button"
    Private Sub RBVista_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RBVista.CheckedChanged
        If RBVista.Checked Then
            BParcelamento.Visible = False
            TSBSalvar.Visible = True
            st = 6 ' a vista
            codccc = 4
            parcela = 1
        Else
            RBPrazo.Checked = True
            TSBSalvar.Visible = False
            st = 4 'prazo
            parcela = 1
            codccc = 7
        End If
    End Sub

    Private Sub RBPrazo_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RBPrazo.CheckedChanged
        If RBPrazo.Checked Then
            BParcelamento.Visible = True
            st = 4  'prazo
            codccc = 7
            parcela = 1
            TSBSalvar.Enabled = False
        Else
            st = 6 'vista
            codccc = 4
            parcela = 1
            TSBSalvar.Enabled = True
        End If
    End Sub
#End Region
#Region "CBRecebimento"
    Private Sub CBFRecebimento_Leave(sender As System.Object, e As System.EventArgs) Handles CBFRecebimento.Leave
        CBFRecebimento_Validating(Nothing, Nothing)
    End Sub
    Private Sub BParcelamento_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles BParcelamento.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
#End Region
#Region "DTPVencimento"
    Private Sub DTPVencimento_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles DTPVencimento.Validating
        validacampos()
    End Sub
    Private Sub DTPVencimento_Leave(sender As System.Object, e As System.EventArgs) Handles DTPVencimento.Leave
        validacampos()
    End Sub
#End Region
#Region "KeyPress"
    Private Sub Contas_ReceberFRM_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
#End Region
#Region "DTVencimento"
    Private Sub DTPVencimento_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DTPVencimento.KeyPress
        valida()
    End Sub
#End Region
End Class